#pragma once

#include "IEndPoint.h"
#include "SimpleCriticalSection.h"

namespace RoomFW {

class EndpointMap
{
public:

	//
	// C/Dtor
	//
	EndpointMap(size_t size);
	~EndpointMap(void);

	//
	// Size of primary index array.
	//
	size_t size(void) const;

	//
	// Resize primary index array.
	//
	void resize(size_t size);

	//
	// Size of replication index array for specified primary index.
	//
	size_t replicationSize(size_t port) const;

	//
	// Number of non-null endpoints for specified primary index.
	//
	size_t replicationCount(size_t port) const;

	//
	// Get the specified endpoint.  May return null.
	//
	IEndPointSP get(size_t port, size_t replication) const; // return endpoint

	//
	// Add an endpoint.  Returns replication index or PortIndex::REPLICATION_BROADCAST on error.
	//
	size_t add(size_t port, IEndPointSP endpoint); // return replication index

	//
	// Set a specific endpoint.  Return success.
	//
	bool set(size_t port, size_t replication, IEndPointSP endpoint); // Set specific port index.

	//
	// Clear a specific endpoint.
	//
	bool clear(size_t port, size_t replication); // return success.

	//
	// Clear all endpoints.
	//
	void clearAll(void);

private:

	typedef std::vector<IEndPointSP> EndpointList;
	typedef std::vector<EndpointList> EndpointList2;

	EndpointList2 endpoints_;
	
	mutable SimpleCriticalSection cs_;
};

}
